<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <!-- <el-page-header
          :content="`${id?'编辑企业':'添加企业'}`"
          @back="$router.back()"
        /> -->
        <span>{{ backBtn }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" :model="addForm" :rules="addRules" label-width="100px">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option
                  v-for="item in industryList"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseId">
              <el-form-item label="租赁合同" prop="contractId">
                <el-upload
                  action="#"
                  :http-request="uploadRequest"
                  :before-upload="beforeAvatarUpload"
                >
                  <el-button size="small" type="primary">上传合同文件</el-button>
                  <div slot="tip" class="el-upload__tip">支持扩展名：.doc .docx .pdf, 文件大小不超过5M</div>
                </el-upload>
              </el-form-item>
            </el-form-item></el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
// import { Upload } from 'element-ui'
// import upload from './Upload.vue'
import { createExterpriseAPI, getIndustryListAPI, updateExterpriseAPI, uploadAPI } from '@/api/Enterprise'

export default {
  data() {
    return {
      industryList: [],
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '' // 营业执照url
        // businessLicenseId: '' // 营业执照id
      },
      backBtn: '',
      addRules: {
        // 企业名称
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        // 法人
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        // 注册地址
        registeredAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ],
        // 所在行业
        industryCode: [
          { required: true, message: '请选择行业', trigger: 'change' }
        ],
        // 企业联系人
        contact: [
          { required: true, message: '请输入企业联系人', trigger: 'blur' }
        ],
        // 联系人电话
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        // 营业执照
        businessLicenseUrl: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  mounted() {
    // 获取行业列表
    this.getIndustryList()
    // 调用企业详情接口
    if (this.id) {
      this.getEnterpriseDetail()
    }
  },
  methods: {
  // 调用接口完成上传
    async uploadRequest(data) {
      const file = data.file
      // 处理formdata类型参数
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'businessLicense')
      const res = await uploadAPI(formData)

      // 赋值表单数据
      this.addForm.businessLicenseId = res.data.id
      this.addForm.businessLicenseUrl = res.data.url
    },
    async uploadHandle(fileData) {
      // 1. 处理FormData
      const { file } = fileData
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'contract')
      // 2. 上传并赋值
      const res = await uploadAPI(formData)
      const { id, url } = res.data
      this.addForm.contractId = id
      this.addForm.contractUrl = url
      // 单独校验表单
      this.$refs.ruleForm.validate('contractId')
    },
    // 获取行业列表
    async getIndustryList() {
      const res = await getIndustryListAPI()
      this.industryList = res.data
      this.backBtn = this.id ? '编辑企业' : '添加企业'
      console.log(this.backBtn)
    },
    // 点击确定提交
    confirmSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        console.log(valid)
        if (this.id) {
        // 编辑
          await updateExterpriseAPI({
            ...this.addForm,
            id: this.id
          })
        } else {
        // 新增
          await createExterpriseAPI(this.addForm)
        }
        this.$router.back()
      })
    },
    // 上传营业执照图片
    beforeAvatarUpload(file) {
      const isImage = ['image/jpeg', 'image/png', 'image/jpg', 'image/gif', 'image/webp'].includes(file.type)
      console.log(isImage)
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isImage) {
        this.$message.error('上传头像图片只能是 JPG、PNG、GIF、WEBP 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isImage && isLt5M
    }
  }
}

</script>

  <style scoped lang="scss">
  .add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      justify-content: space-between;
      padding: 0 20px;
      height: 64px;
      line-height: 64px;
      font-size: 16px;
      font-weight: bold;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;
        .title {
          height: 60px;
          line-height: 60px;
          padding-left:20px;
        }
        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;
          .el-form{
            display: flex;
            flex-wrap: wrap;
            .el-form-item{
              width: 50%;
            }
          }
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  </style>
